﻿using SimWithP3d.Vehicle;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;

namespace SimWithP3d.ViewModel
{
    public class RealDataViewModel : NotifyChanged
    {
        private int aileron_Axis;
        public int Aileron_Axis
        {
            get { return aileron_Axis; }
            set { aileron_Axis = value; OnPropertyChanged(new PropertyChangedEventArgs("Aileron_Axis")); }
        }
        private int elevator_Axis;
        public int Elevator_Axis
        {
            get { return elevator_Axis; }
            set { elevator_Axis = value; OnPropertyChanged(new PropertyChangedEventArgs("Elevator_Axis")); }
        }
        private int rudder_Axis;
        public int Rudder_Axis
        {
            get { return rudder_Axis; }
            set { rudder_Axis = value; OnPropertyChanged(new PropertyChangedEventArgs("Rudder_Axis")); }
        }

        private int left_Brake;
        public int Left_Brake
        {
            get { return left_Brake; }
            set { left_Brake = value; OnPropertyChanged(new PropertyChangedEventArgs("Left_Brake")); }
        }

        private int right_Brake;
        public int Right_Brake
        {
            get { return right_Brake; }
            set { right_Brake = value; OnPropertyChanged(new PropertyChangedEventArgs("Right_Brake")); }
        }
        private int throttle;
        public int Throttle
        {
            get { return throttle; }
            set { throttle = value; OnPropertyChanged(new PropertyChangedEventArgs("Throttle")); }
        }

        private int mixture;
        public int Mixture
        {
            get { return mixture; }
            set { mixture = value; OnPropertyChanged(new PropertyChangedEventArgs("Mixture")); }
        }
        private int rudder_Trim;
        public int Rudder_Trim
        {
            get { return rudder_Trim; }
            set { rudder_Trim = value; OnPropertyChanged(new PropertyChangedEventArgs("Rudder_Trim")); }
        }
        private int elevator_Trim;
        public int Elevator_Trim
        {
            get { return elevator_Trim; }
            set { elevator_Trim = value; OnPropertyChanged(new PropertyChangedEventArgs("Elevator_Trim")); }
        }
        //region

        private int master_Battery_Switch;
        public int Master_Battery_Switch
        {
            get { return master_Battery_Switch; }
            set { master_Battery_Switch = value; OnPropertyChanged(new PropertyChangedEventArgs("Master_Battery_Switch")); }
        }
        private int engine_Primer;
        public int Engine_Primer
        {
            get { return engine_Primer; }
            set { engine_Primer = value; OnPropertyChanged(new PropertyChangedEventArgs("Engine_Primer")); }
        }
        private int magneto;
        public int Magneto
        {
            get { return magneto; }
            set { magneto = value; OnPropertyChanged(new PropertyChangedEventArgs("Magneto")); }
        }
        private int pitot_Heat;
        public int Pitot_Heat
        {
            get { return pitot_Heat; }
            set { pitot_Heat = value; OnPropertyChanged(new PropertyChangedEventArgs("Pitot_Heat")); }
        }
        private int autopilot_Switch;
        public int Autopilot_Switch
        {
            get { return autopilot_Switch; }
            set { autopilot_Switch = value; OnPropertyChanged(new PropertyChangedEventArgs("Autopilot_Switch")); }
        }
        private int fuel_Tank_Selector;
        public int Fuel_Tank_Selector
        {
            get { return fuel_Tank_Selector; }
            set { fuel_Tank_Selector = value; OnPropertyChanged(new PropertyChangedEventArgs("Fuel_Tank_Selector")); }
        }
        private int elt_Switch;
        public int Elt_Switch
        {
            get { return elt_Switch; }
            set { elt_Switch = value; OnPropertyChanged(new PropertyChangedEventArgs("Elt_Switch")); }
        }
        private int lights_Switch;
        public int Lights_Switch
        {
            get { return lights_Switch; }
            set { lights_Switch = value; OnPropertyChanged(new PropertyChangedEventArgs("Lights_Switch")); }
        }
        private int fuel_Pump;
        public int Fuel_Pump
        {
            get { return fuel_Pump; }
            set { fuel_Pump = value; OnPropertyChanged(new PropertyChangedEventArgs("Fuel_Pump")); }
        }
        private int alternator;
        public int Alternator
        {
            get { return alternator; }
            set { alternator = value; OnPropertyChanged(new PropertyChangedEventArgs("Alternator")); }
        }
        private int flaps;
        public int Flaps
        {
            get { return flaps; }
            set { flaps = value; OnPropertyChanged(new PropertyChangedEventArgs("Flaps")); }
        }
        private int carb_heat;
        public int Carb_heat
        {
            get { return carb_heat; }
            set { carb_heat = value; OnPropertyChanged(new PropertyChangedEventArgs("Carb_heat")); }
        }
        private int parking;
        public int Parking
        {
            get { return parking; }
            set { parking = value; OnPropertyChanged(new PropertyChangedEventArgs("Parking")); }
        }
        private int avionics_Master;
        public int Avionics_Master
        {
            get { return avionics_Master; }
            set { avionics_Master = value; OnPropertyChanged(new PropertyChangedEventArgs("Avionics_Master")); }
        }
        private int gearUpDown;
        public int GearUpDown
        {
            get { return gearUpDown; }
            set { gearUpDown = value; OnPropertyChanged(new PropertyChangedEventArgs("GearUpDown")); }
        }
        private int pause;
        public int Pause
        {
            get { return pause; }
            set { pause = value; OnPropertyChanged(new PropertyChangedEventArgs("Pause")); }
        }    
        /// <summary>
        /// 根据协议解析成相应协议类型
        /// </summary>
        public void ParseBytesToStr(byte[] data, out int nodeId, out int mark)
        {
            //区域编号
            nodeId = 0;
            nodeId |= data[1];
            //mark
            byte[] markBytes = new byte[2];
            Array.ConstrainedCopy(data, 4, markBytes, 0, 2);
            mark = ByteToHexStr(markBytes);
        }
        /// <summary>
        /// 字节数组转16进制字符串
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public int ByteToHexStr(byte[] bytes)
        {
            string returnStr = "";
            if (bytes != null)
            {
                for (int i = 0; i < bytes.Length; i++)
                {
                    returnStr += bytes[i].ToString("X2");
                }
            }
            return int.Parse(returnStr, System.Globalization.NumberStyles.HexNumber);
        }
        internal void LowerDevicesDataDeal(byte[] rData)
        {
            try
            {
                int nodeId;//区域编号
                int mark;//代号（2字节）
                ParseBytesToStr(rData, out nodeId, out mark);

                byte[] datas = new byte[6];
                Array.ConstrainedCopy(rData, 6, datas, 0, 6);

                switch (nodeId)
                {
                    case 01:
                        switch (mark)
                        {
                            case 1:
                                SwitchPanel1(datas);
                                break;
                            case 2:
                                SwitchPanel2(datas);
                                break;
                            case 5001:
                                byte[] throttle = new byte[3];
                                Array.ConstrainedCopy(datas, 0, throttle, 0, 3);
                                Throttle = (short)ByteToHexStr(throttle);//油门杆
                                break;
                            case 5002:
                                byte[] mixture = new byte[3];
                                Array.ConstrainedCopy(datas, 0, mixture, 0, 3);
                                Mixture = (short)ByteToHexStr(mixture);//油门混合比杆
                                break;
                            case 5003:
                                byte[] rudder = new byte[3];
                                Array.ConstrainedCopy(datas, 0, rudder, 0, 3);

                                byte[] elevator = new byte[3];
                                Array.ConstrainedCopy(datas, 3, elevator, 0, 3);
                                Rudder_Trim = (short)(ByteToHexStr(rudder) - 16383);
                                Elevator_Trim = (short)(ByteToHexStr(elevator) - 16383);
                                break;
                            default:
                                break;
                        }
                        break;
                    case 02:
                        JoystickAndPedal(mark, datas);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                App.RecordErrorLog(ex);
            }
        }

        private void SwitchPanel2(byte[] datas)
        {
           
        }

        /// <summary>
        ///区域02
        /// </summary>
        private void JoystickAndPedal(int mark, byte[] datas)
        {
            byte[] hight3 = new byte[3];
            Array.ConstrainedCopy(datas, 0, hight3, 0, 3);

            byte[] low3 = new byte[3];
            Array.ConstrainedCopy(datas, 3, low3, 0, 3);

            switch (mark)
            {
                case 5004://脚蹬数据
                    Rudder_Axis = (short)(ByteToHexStr(hight3) - 16383);
                    break;
                case 5005://驾驶杆数据
                    Aileron_Axis = (short)(ByteToHexStr(hight3) - 16383);
                    Elevator_Axis = (short)(ByteToHexStr(low3) - 16383);
                    break;
                case 5007://踏板数据
                    Left_Brake = (short)ByteToHexStr(hight3);
                    Right_Brake = (short)ByteToHexStr(low3);
                    break;
                default:
                    break;
            }
        }
        /// <summary>
        /// mark1 开关面板 masterALT BAT 开关,avionics BUS1 bus2开关，灯光组开关、FUEL PUMP，PITOT HEAT，CABIN PEWR，STBY BATT
        /// </summary>
        private void SwitchPanel1(byte[] datas)
        {
            BitArray panel_1 = new BitArray(datas);
            //蓄电池
            byte Master_Battery_Switch_Byte = (byte)(panel_1[0] ? 1 : 0);
            if (Master_Battery_Switch != Master_Battery_Switch_Byte)
            {
                Master_Battery_Switch = Master_Battery_Switch_Byte;
            }
            //发动机启动注油器
            Engine_Primer = (byte)(panel_1[1] ? 1 : 0);
            //钥匙开关
            Magneto = panel_1[2] ? (short)4 : (short)3;
            Magneto = panel_1[12] ? (short)1 : Magneto;
            Magneto = panel_1[13] ? (short)2 : Magneto;
            Magneto = panel_1[12] && panel_1[13] ? (short)3 : Magneto;
            //空速管加温
            Pitot_Heat = (byte)(panel_1[3] ? 1 : 0);
            //自动驾驶仪开关
            Autopilot_Switch = (byte)(panel_1[4] ? 1 : 0);
            //油箱选择 2=Left, 3=Right
            Fuel_Tank_Selector = (byte)(panel_1[5] ? 3 : 2);
            Console.WriteLine(Fuel_Tank_Selector);
            //油箱选择 1=All
            Fuel_Tank_Selector = (byte)(panel_1[6] ? 1 : Fuel_Tank_Selector);
            //应急发射转换开关
            Elt_Switch = (byte)(panel_1[7] ? 1 : 0);
            //灯开关1=Nav, 2=Bcn, 4=Land, 8=Taxi, 16=Strobe, 32=Panel, 64=Recognition, 128=Wing, 256=Logo, 512=Cabin
            short Lights_Switch_Value = 0;
            if (panel_1[8])
            {
                Lights_Switch_Value += 512;
            }
            if (panel_1[9])
            {
                Lights_Switch_Value += 16;
            }
            if (panel_1[10])
            {
                Lights_Switch_Value += 1;
            }
            if (panel_1[11])
            {
                Lights_Switch_Value += 4;
            }
            Lights_Switch = Lights_Switch_Value;
            //燃油泵开关
            Fuel_Pump = (byte)(panel_1[14] ? 1 : 0);
            //交流发电机
            Alternator = (byte)(panel_1[15] ? 1 : 0);
            //FLap Position 0、1、2、3 对应数值  0 ，5461、 10922 、 16383
            Flaps = panel_1[18] ? 5461 : 0;
            Flaps = panel_1[19] && !panel_1[18] ? 10922 : Flaps;
            Flaps = panel_1[19] && panel_1[18] ? 16383 : Flaps;
            //化油器热控制
            Carb_heat = panel_1[20] ? (short)1 : (short)0;
            //手刹Parking brake: 0=off, 32767=on
            Parking = panel_1[21] ? (short)32767 : (short)0;

            //主要电子设备开关
            Avionics_Master = panel_1[22] ? (short)1 : (short)0;
            //起落架
            GearUpDown = panel_1[33] ? (short)1 : (short)0;
            //模拟器暂停/运行
            Pause = panel_1[34] ? (short)1 : (short)0;
        }
    }
}
